const express = require('express')

const app = express()

app.use('*', (req, res, next) => {
	res.setHeader('Access-Control-Allow-Origin', 'http://127.0.0.1:5605')
	res.setHeader('Access-Control-Allow-Methods', 'GET,POST,PATCH,DELETE,PUT')
	res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type')
	next()
})

app.get('/sse', function (req, res) {
	// TODO 5 - sse 单工通讯 ,适合大屏项目
	res.setHeader('Content-Type', 'text/event-stream')
	setInterval(() => {
		// 修改事件名称
		// res.write("event: message\n");
		res.write('event: test\n')

		// data + 数据
		res.write('data: ' + new Date().toLocaleString() + '\n\n')
	}, 1000)
})

app.listen(8000, () => {
	console.log('http://localhost:8000')
})
